<template>
  <div class="skill-detail-page">
    <!-- 顶部导航栏 -->
    <TopNavbar 
      @navigate="handleNavigation" 
      @show-login="showLogin"
      @show-register="showRegister"
      @logout="handleLogout"
    />
    
    <!-- 返回按钮 -->
    <div class="back-button-container">
      <button class="back-btn" @click="goBack">
        <span class="back-icon">←</span>
        返回英雄页面
      </button>
    </div>

    <!-- 技能详情内容 -->
    <div class="skill-detail-container" v-if="skill">
      <div class="skill-detail-header">
        <h1 class="skill-title">{{ skill.skillName }}</h1>
        <div class="hero-info" v-if="heroInfo">
          <img :src="heroInfo.smallIconUrl || defaultAvatarImage" :alt="heroInfo.heroName" class="hero-avatar" />
          <span class="hero-name">{{ heroInfo.heroName }}</span>
        </div>
      </div>

      <div class="skill-content">
        <!-- 技能GIF展示 -->
        <div class="skill-gif-section" v-if="skill.gifUrl">
          <h3 class="section-title">技能展示</h3>
          <div class="gif-container">
            <img :src="skill.gifUrl" :alt="skill.skillName + ' GIF'" class="skill-gif" />
          </div>
        </div>

        <!-- 技能视频播放 -->
        <div class="skill-video-section" v-if="skill.videoUrl">
          <h3 class="section-title">技能视频</h3>
          <div class="video-container">
            <video 
              :src="skill.videoUrl" 
              controls 
              class="skill-video"
              :poster="skill.gifUrl"
            >
              您的浏览器不支持视频播放。
            </video>
          </div>
        </div>

        <!-- 技能描述 -->
        <div class="skill-description-section">
          <h3 class="section-title">技能描述</h3>
          <div class="skill-description-full">
            <p>{{ skill.skillDescription || '暂无技能描述' }}</p>
          </div>
        </div>


      </div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载技能详情中...</p>
    </div>

    <!-- 错误状态 -->
    <div v-if="error" class="error-container">
      <p>{{ error }}</p>
      <button class="retry-btn" @click="loadSkillDetail">重试</button>
    </div>
  </div>
</template>

<script>
import TopNavbar from './TopNavbar.vue'
import { skillApi } from '../api/skill.js'
import { heroApi } from '../api/hero.js'

export default {
  name: 'SkillDetail',
  components: {
    TopNavbar
  },
  data() {
    return {
      skill: null,
      heroInfo: null,
      loading: true,
      error: null,
      defaultAvatarImage: 'https://fastcdn.mihoyo.com/content-v2/bh3/156251/ea55b240730debec811239cbb393a7bb_2296978660569028145.png'
    }
  },
  async mounted() {
    await this.loadSkillDetail()
  },
  methods: {
    async loadSkillDetail() {
      try {
        this.loading = true
        this.error = null
        
        const skillId = this.$route.params.skillId
        if (!skillId) {
          throw new Error('技能ID不存在')
        }

        // 获取技能详情
        const skillResponse = await skillApi.getSkillById(skillId)
        if (skillResponse.code === 0) {
          this.skill = skillResponse.data
          
          // 如果有英雄ID，获取英雄信息
          if (this.skill.heroId) {
            const heroResponse = await heroApi.getHeroById(this.skill.heroId)
            if (heroResponse.code === 0) {
              this.heroInfo = heroResponse.data
            }
          }
        } else {
          throw new Error(skillResponse.message || '获取技能详情失败')
        }
      } catch (error) {
        console.error('加载技能详情失败:', error)
        this.error = error.message || '加载技能详情失败'
      } finally {
        this.loading = false
      }
    },
    
    goBack() {
      // 返回英雄页面，如果有英雄ID则跳转到对应英雄
      if (this.skill?.heroId) {
        this.$router.push({ name: 'heroes', query: { heroId: this.skill.heroId } })
      } else {
        this.$router.push({ name: 'heroes' })
      }
    },
    
    handleNavigation(page) {
      this.$router.push({ name: page })
    },
    
    showLogin() {
      // 显示登录弹窗逻辑
    },
    
    showRegister() {
      // 显示注册弹窗逻辑
    },
    
    handleLogout() {
      // 登出逻辑
    }
  }
}
</script>

<style scoped>
.skill-detail-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.back-button-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: white;
  text-decoration: none;
  transition: all 0.3s ease;
  cursor: pointer;
  font-size: 14px;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-2px);
}

.back-icon {
  font-size: 16px;
}

.skill-detail-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 40px;
}

.skill-detail-header {
  text-align: center;
  margin-bottom: 40px;
  padding: 30px 0;
  border-bottom: 2px solid rgba(255, 255, 255, 0.1);
}

.skill-title {
  font-size: 36px;
  font-weight: 700;
  color: #4ecdc4;
  margin: 0 0 20px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.hero-info {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 25px;
  backdrop-filter: blur(10px);
}

.hero-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #4ecdc4;
}

.hero-name {
  font-size: 16px;
  font-weight: 600;
  color: #4ecdc4;
}

.skill-content {
  display: grid;
  gap: 40px;
}

.section-title {
  font-size: 24px;
  font-weight: 600;
  color: #4ecdc4;
  margin: 0 0 20px 0;
  text-align: center;
}

.skill-gif-section {
  text-align: center;
}

.gif-container {
  display: inline-block;
  padding: 20px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.skill-gif {
  max-width: 100%;
  max-height: 400px;
  border-radius: 10px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.skill-video-section {
  text-align: center;
}

.video-container {
  display: inline-block;
  padding: 20px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.skill-video {
  width: 100%;
  max-width: 800px;
  height: auto;
  border-radius: 10px;
}

.skill-description-section {
  background: rgba(255, 255, 255, 0.05);
  padding: 30px;
  border-radius: 15px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.skill-description-full {
  font-size: 16px;
  line-height: 1.8;
  color: rgba(255, 255, 255, 0.9);
  text-align: justify;
}

.skill-description-full p {
  margin: 0;
}



.loading-container, .error-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 400px;
  text-align: center;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top: 4px solid #4ecdc4;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.retry-btn {
  padding: 10px 20px;
  background: #4ecdc4;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  margin-top: 10px;
  transition: all 0.3s ease;
}

.retry-btn:hover {
  background: #44a08d;
  transform: translateY(-2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .skill-title {
    font-size: 28px;
  }
  
  .skill-detail-container {
    padding: 0 15px 30px;
  }
}
</style>